define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wwh/archives/index' + location.search,
                    add_url: 'wwh/archives/add?lang=1',
                    edit_url: 'wwh/archives/edit',
                    del_url: 'wwh/archives/del',
                    multi_url: 'wwh/archives/multi',
                    import_url: 'wwh/archives/import',
                    table: 'wwh_archives',
                    dragsort_url:'',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                queryParams: function (params) {
                    var langId = $('ul.nav-tabs li.active a').data('value') || 1;
                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    filter.lang = langId;
                    var columnId = $(".commonsearch-table input[name=column_id]").val() || '';

                    // 处理空值和全选情况
                    if (columnId) {
                        filter.column_id = columnId;
                    } else {
                        // 当没有选择任何分类时，不添加column_id条件
                        delete filter.column_id;
                    }

                    params.filter = JSON.stringify(filter);

                    var op = params.op ? JSON.parse(params.op) : {};
                    op.lang = "=";

                    // 使用IN操作符处理多个ID
                    if (columnId) {
                        op.column_id = "IN";
                    } else {
                        delete op.column_id;
                    }

                    params.op = JSON.stringify(op);

                    return params;
                },
                columns: [
                    [
                        {
                            checkbox: true, formatter:function (value,row,index){
                                if (row.status == 1){
                                    return{
                                        disabled:true
                                    };
                                }
                            }
                        },
                        {field: 'id', title: __('Id')},
                        {
                            field: 'column',
                            title: __('Column_id'),
                            formatter: function (val,data) {
                                var lang = val.name;
                                return lang;
                            }
                        },
                        {
                            field: 'column_id',
                            title: __('Column_id'),
                            visible: false,
                            addclass: 'selectpage',
                            extend: 'data-source="wwh/column/index" data-field="name"',
                            operate: 'in',
                            formatter: Table.api.formatter.search
                        },
                        {
                            field: 'title', title: __('Title'), align: 'left', operate: 'like', customField: 'rec_data', formatter: function (value, row, index) {
                                var flagObj = $.extend({}, this, {searchList: {"0":__('Rec_data 0'),"1":__('Rec_data 1'),"10":__('Rec_data 10')},custom: {0: 'primary', 1: 'success', 10: 'warning'}});
                                if (row.rec_data == 0){
                                    return '<div class="archives-title"><a href="' + row.url + '" target="_blank"><span style="color:' + (row.style_color ? row.style_color : 'inherit') + ';font-weight:' + (row.style_bold ? 'bold' : 'normal') + '">' + value + '</span></a></div>';
                                } else {
                                    return '<div class="archives-title"><a href="' + row.url + '" target="_blank"><span style="color:' + (row.style_color ? row.style_color : 'inherit') + ';font-weight:' + (row.style_bold ? 'bold' : 'normal') + '">' + value + '</span></a></div>' +
                                        '<div class="archives-label">' + Table.api.formatter.flag.call(flagObj, row['rec_data'], row, index) + '</div>';
                                }
                            }
                        },
                        {field: 'rec_data', title: __('Rec_data'), operate: 'find_in_set', visible: false, searchList: Config.recDataList, formatter: Table.api.formatter.flag},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {
                            field: 'weigh',
                            title: __('Weigh'),
                            operate: false,
                            sortable: true,
                            formatter: function (value, row, index) {
                                return '<input type="text" class="form-control text-center text-weigh" data-id="' + row.id + '" value="' + value + '" style="width:50px;margin:0 auto;" />';
                            },
                            events: {
                                "dblclick .text-weigh": function (e) {
                                    e.preventDefault();
                                    e.stopPropagation();
                                    return false;
                                }
                            }
                        },
                        {field: 'views', title: __('Views')},
                        {field: 'time', title: __('Time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, custom: {0: 'grey', 1: 'info'}, formatter: Table.api.formatter.flag},
                        {field: 'lang', title: __('Lang'), searchList: {"1":__('Lang 1'),"2":__('Lang 2')}, custom: {1: 'success', 2: 'warning'}, formatter: Table.api.formatter.normal},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'faudit',
                                    text: __('反审核'),
                                    title: __('反审核'),
                                    classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                    // icon: 'fa fa-close',
                                    url: 'wwh/archives/faudit',
                                    hidden:function(row){
                                        if(row.status != '1'){
                                            return true;
                                        }
                                    },
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh');    //自动刷新
                                    },
                                }
                            ],
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                if (row.status == 1){
                                    $(table).data("operate-edit",null);
                                    $(table).data("operate-del",null);
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }}
                    ]
                ]
            });

            // 权重修改
            $(document).on("change", ".text-weigh", function () {
                $(this).data("params", {weigh: $(this).val()});
                Table.api.multi('', [$(this).data("id")], table, this);
                return false;
            });

            // 审核
            $(document).on("click", ".btn-audit", function () {
                var data = table.bootstrapTable('getSelections');
                var ids = [];
                if (data.length === 0) {
                    Toastr.error("请选择操作信息");
                    return;
                }
                for (var i = 0; i < data.length; i++) {
                    ids[i] = data[i]['id']
                }
                Layer.confirm(
                    '确认选中'+ids.length+'条审核吗?',
                    {icon: 3, title: __('Warning'), offset: '40%', shadeClose: true},
                    function (index) {
                        Layer.close(index);
                        Backend.api.ajax({
                            url: "wwh/archives/audit",
                            data: {ids:ids}
                        }, function(data, ret){//成功的回调
                            if (ret.code === 1) {
                                table.bootstrapTable('refresh');
                                Layer.close(index);
                            } else {
                                Layer.close(index);
                                Toastr.error(ret.msg);
                            }
                        }, function(data, ret){//失败的回调
                            console.log(ret);
                            Layer.close(index);
                        });
                    }
                );
            });

            // tab切换时重载栏目并清空栏目选中
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var langId = $(this).data('value');

                var options = table.bootstrapTable('getOptions');
                options.extend.add_url = 'wwh/archives/add?lang=' + langId;

                updateColumnTree(langId, function() {
                    $(".commonsearch-table input[name=column_id]").val('');
                    $("#table").bootstrapTable('refresh', {});
                });
            });

            // 动态加载栏目
            function updateColumnTree(lang, callback) {
                require(['jstree'], function () {
                    $.ajax({
                        url: 'wwh/archives/getColumnList',
                        type: 'GET',
                        data: {lang: lang},
                        dataType: 'json',
                        success: function(response) {
                            if (response.code == 1) {
                                if ($('#columntree').hasClass('jstree')) {
                                    $('#columntree').jstree('destroy');
                                }
                                $('#columntree').jstree({
                                    "themes": {"stripes": true},
                                    "checkbox": {"keep_selected_style": false},
                                    "types": {
                                        "column": {"icon": false},
                                        "list": {"icon": false},
                                        "link": {"icon": false},
                                        "disabled": {"check_node": false, "uncheck_node": false}
                                    },
                                    'plugins': ["types", "checkbox"],
                                    "core": {
                                        "multiple": true,
                                        'check_callback': true,
                                        "data": response.data
                                    }
                                })
                                    .on('ready.jstree', function (e, data) {
                                        // 初始化展开状态
                                        if ($("#expandall").prop("checked")) {
                                            $(this).jstree('open_all');
                                        }
                                        // 初始化选中状态
                                        if ($("#checkall").prop("checked")) {
                                            $(this).jstree('select_all');
                                        }
                                    })
                                    .off("changed.jstree").on("changed.jstree", function (e, data) {
                                    // 优化递归函数：确保获取所有节点
                                    function getAllDescendantIds(node) {
                                        var ids = [node.id];
                                        if (node.children && node.children.length > 0) {
                                            for (var i = 0; i < node.children.length; i++) {
                                                var childNode = $('#columntree').jstree('get_node', node.children[i]);
                                                ids = ids.concat(getAllDescendantIds(childNode));
                                            }
                                        }
                                        return ids;
                                    }

                                    var selectedIds = data.selected;
                                    var allIds = [];

                                    if (selectedIds.length === 0) {
                                        // 当没有选中任何节点时，显示所有内容
                                        $(".commonsearch-table input[name=column_id]").val('');
                                    } else {
                                        for (var i = 0; i < selectedIds.length; i++) {
                                            var node = $('#columntree').jstree('get_node', selectedIds[i]);
                                            allIds = allIds.concat(getAllDescendantIds(node));
                                        }
                                        allIds = Array.from(new Set(allIds));
                                        $(".commonsearch-table input[name=column_id]").val(allIds.join(","));
                                    }

                                    $("#table").bootstrapTable('refresh', {});
                                    return false;
                                });
                                if (typeof callback === 'function') callback();
                            }
                        }
                    });
                });
            }

            // 绑定全选事件
            $("#checkall").on('change', function() {
                if ($('#columntree').hasClass('jstree')) {
                    if ($(this).prop("checked")) {
                        $('#columntree').jstree('select_all');
                    } else {
                        $('#columntree').jstree('deselect_all');
                    }
                }
            });

            // 绑定展开事件
            $("#expandall").on('change', function() {
                if ($('#columntree').hasClass('jstree')) {
                    if ($(this).prop("checked")) {
                        $('#columntree').jstree('open_all');
                    } else {
                        $('#columntree').jstree('close_all');
                    }
                }
            });

            // 页面初始化时加载默认语言栏目
            $(function(){
                var defaultLang = $('ul.nav-tabs li.active a').data('value') || 1;
                updateColumnTree(defaultLang);
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 弹窗窗口大小
            table.on('post-body.bs.table',function(e, settings, json, xhr){
                $(".btn-add").data("area", ['80%','80%']);
                $(".btn-editone").data("area", ['80%','80%']);
            });

            // TAB选项卡默认选中简体中文
            $('ul.nav-tabs li a[data-toggle="tab"]').eq(0).trigger("click");

            // 获取TAB选项卡值,通过options.extend.add_url传递
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var options = table.bootstrapTable('getOptions');
                options.extend.add_url = 'wwh/archives/add?lang=' + $(this).data('value');
            });

            $(document).on("click", "a.btn-column", function () {
                $("#archivespanel").toggleClass("col-md-9", $("#columnbar").hasClass("hidden"));
                $("#archivespanel").toggleClass("col-full-width", !$("#columnbar").hasClass("hidden"));
                $("#columnbar").toggleClass("hidden");
            });
        },
        add: function () {
            // 将TAB选项卡赋值给lang
            $('#c-lang option').each(function(){
                if($(this).attr('value') == Fast.api.query('lang'))
                {
                    $(this).attr('selected', 'selected');
                }
            });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
            $("input[name='row[type]']:checked").trigger("fa.event.typeupdated", "edit");
        },
        api: {
            formatter: {
                subnode: function (value, row, index) {
                    return '<a href="javascript:;" data-toggle="tooltip" title="' + __('Toggle sub menu') + '" data-id="' + row.id + '" data-parent_id="' + row.parent_id + '" class="btn btn-xs '
                        + (row.haschild == 1 || row.ismenu == 1 ? 'btn-success' : 'btn-default disabled') + ' btn-node-sub"><i class="fa fa-' + (row.haschild == 1 || row.ismenu == 1 ? 'sitemap' : 'list') + '"></i></a>';
                }
            },
            bindevent: function () {
                // 根据类型显示隐藏对应字段
                $(document).on("click fa.event.typeupdated", "input[name='row[type]']", function (e, ref) {
                    $(".tf").addClass("hidden");
                    $(".tf.tf-" + $(this).val()).removeClass("hidden");
                });
                // 不验证隐藏元素
                $('form[role=form]').data("validator-options", {
                    ignore: ':hidden'
                });

                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
